<?php

class Mozg_Lang
{
	static public $active = 'ru';

	static public $langs = array('ru', 'en');

	private function __construct(){}

	static private $_langInstances = array();

	static public function _($text)
	{
		$params = array();
		if (func_num_args() > 1)
		{
			$params = func_get_args();
			unset($params[0]);
		}

		return self::Translate(self::GetLang(), $text, $params);
	}

	static public function _L($lang, $text)
	{
		$params = array();
		if (func_num_args() > 2)
		{
			$params = func_get_args();
			unset($params[0]);
			unset($params[1]);
		}

		return self::Translate($lang, $text, $params);
	}

	static private function Translate($lang, $text, $params)
	{
		if (is_array($text))
		{
			foreach ($text as &$str)
			{
				$str = self::GetLangInstance($lang)->Translate($str, $params, self::IsDebugMode());
			}
		}
		else
		{
			$text = self::GetLangInstance($lang)->Translate($text, $params, self::IsDebugMode());
		}

		return $text;
	}

	static private function GetLangInstance($lang)
	{
		if (!isset(self::$_langInstances[$lang]))
		{
			self::$_langInstances[$lang] = new Mozg_Lang_Instance(_APP_PATH. 'lang/' .$lang);
		}

		return self::$_langInstances[$lang];
	}

	static public function Required($langPackName, $lang = false)
	{
		if (!$lang) $lang = self::GetLang();
		self::GetLangInstance($lang)->Required($langPackName);
	}

	static private $_lang = false;

	static public function SetLang($lang)
	{
		if (!$lang) return;
		self::$_lang = $lang;
	}

	static public function GetLang()
	{
		//if (!self::$_lang) self::$_lang = Mozg_Cfg::Get('global')->GetLang();
		return self::$active;
	}

	static private $_debugMode = null;

	static public function SetDebugMode($mode = true)
	{
		self::$_debugMode = (bool)$mode;
	}

	static public function IsDebugMode()
	{
		if (is_null(self::$_debugMode)) self::$_debugMode = (bool)Mozg_Cfg::Get('global')->GetDebugLang();
		return self::$_debugMode;
	}
}